package com.tskj.mh.tabletennis.dao.redis;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis配置类
 * @program: springbootdemo
 * @Date: 2019/2/22 15:20
 * @Author: zjjlive
 * @Description:
 */
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public RedisSerializer fastJson2JsonRedisSerializer() {
        return new FastJson2JsonRedisSerializer<>(Object.class);
    }

    @Bean
    public RedisTemplate initRedisTemplate(RedisConnectionFactory redisConnectionFactory, RedisSerializer fastJson2JsonRedisSerializer) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(fastJson2JsonRedisSerializer);
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(fastJson2JsonRedisSerializer);
        redisTemplate.setDefaultSerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    /**
     * retemplate相关配置
     * @param factory
     * @return
     */
//    @Bean
//    public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory factory, RedisSerializer fastJson2JsonRedisSerializer) {
//        RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(factory);
//
//        //key采用String序列化方式
//        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//        redisTemplate.setKeySerializer(stringRedisSerializer);
//        redisTemplate.setHashKeySerializer(stringRedisSerializer);
//
//        //value采用fast-json序列化方式。
//        redisTemplate.setValueSerializer(fastJson2JsonRedisSerializer);
//        redisTemplate.setHashValueSerializer(fastJson2JsonRedisSerializer);
//
//        redisTemplate.afterPropertiesSet();
//        return redisTemplate;
//    }
//
//    @Bean
//    public RedisSerializer fastJson2JsonRedisSerializer() {
//        return new FastJson2JsonRedisSerializer<>(Object.class);
//    }
//
//
//    public static class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> {
//
//        public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
//
//        private Class<T> clazz;
//
//        public FastJson2JsonRedisSerializer(Class<T> clazz) {
//            super();
//            this.clazz = clazz;
//        }
//
//        @Override
//        public byte[] serialize(T t) throws SerializationException {
//            if (t == null) {
//                return new byte[0];
//            }
//            return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
//        }
//
//        @Override
//        public T deserialize(byte[] bytes) throws SerializationException {
//            if (bytes == null || bytes.length <= 0) {
//                return null;
//            }
//            String str = new String(bytes, DEFAULT_CHARSET);
//            return JSON.parseObject(str, clazz, Feature.SupportAutoType);
//        }
//
//    }
}
